package task1and3;
/*
 *   定义一个学生类Student，包含属性：姓名（String name)、年龄（int age）
 * */
import java.util.Objects;

public class Student {
    //1.私有化成员变量（姓名和年龄）
    private String name;
    private int age;

    //3.公有化有参与无参构造方法
    public Student() {
    }
    public Student(String name, int age) {
        setName(name);
        setAge(age);
    }

    //2.公有化set与get方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if(age < 0 || age > 150) {
            System.out.println("您的年龄不合理哦！！！");
        } else {
            this.age = age;
        }
    }

    //4.重写toString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //5.重写equals方法和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
